jQuery(document).ready(function() {
	$('#kit-search-more').on('click', function() {
		$('.kit-search-mored').toggle();
	});
	bindFuzerenSelect2();
	var col = [ 
	{
	checkbox: 'checkbox'
	},
	{
		title:'id',
		field:'id',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'openid',
		field:'openid',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'学员id',
		field:'studentId',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'学员英文名',
		field:'enName',
		align: 'center',
		valign: 'middle',
        formatter: function (value, row, index) {
            return '<a href="javascript:;" style="color: red;" onclick="detail(\''+row.studentId+'\')">'+row.enName+'</a>';
        }
	},
	{
		title:'Zoom账号',
		field:'zoomId',
		align: 'center',
		valign: 'middle'
	},
	{
		title:'电话',
		field:'phone',
		align: 'center',
		valign: 'middle'
	},
	{
		title:'负责人id',
		field:'fuzerenId',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'负责人',
		field:'fuzeren',
		align: 'center',
		valign: 'middle'
	},
	{
		// title:'推荐总奖励（小时）',
		title:'已推荐（个数）',
		field:'totalAward',
		align: 'center',
		valign: 'middle'
	},
	{
		// title:'已发放奖励（小时）',
		title:'已发放奖励 （个数）',
		field:'grantedAward',
		align: 'center',
		valign: 'middle'
	},
	{
		// title:'待发放奖励（小时）',
		title:'待发放奖励（个数）',
		field:'remainedAward',
		align: 'center',
		valign: 'middle'
	},
	{
		title:'推荐成功（次）',
		field:'successTimes',
		align: 'center',
		valign: 'middle'
	},
	{
		title: '操作',
	    formatter: function (value, row, index) {
	    	console.log('row', row);
		    return '<a href="javascript:;" onclick="recommendMsg(\''+row.studentId+'\')" class="layui-btn layui-btn-sm layui-btn">推荐信息</a>';
	    } //自定义方法，添加操作按钮
	
	}];

	//------------------------------------------加载列表
	$('#_table').bootstrapTable({
		url:baseUrl+'/recommends',
		'method': "GET",
		locale: 'zh-CN',
		detailView: false,
		sidePagination: "server", //服务端处理分页
		striped: true,//隔行变色
		undefinedText: "",
		pagination: true,
		singleSelect: false,
		pageSize: pageList[0],
		cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
		pageNumber: 1,
		pageList: pageList,
		search: false, //不显示 搜索框
		uniqueId: "studentId", //每一行的唯一标识，一般为主键列
		showColumns: true, //不显示下拉框（选择显示的列）
		undefinedText: '',
		columns: col,
		search: true,
		showRefresh: true,
		queryParams: function(params) {
			return {
				pageNum: this.pageNumber,
				pageSize: this.pageSize,
				sortOrder: params.order
			}
		},
		
	});

	// 2018-9-26 chenbin: 负责人下拉改变事件，刷新列表显示
	document.getElementById("_fuzerenSelect").onchange=function(){		
		fuzerenId = this.value;
		var selectCondition;
		if(fuzerenId=="all"){
			$("#_table").bootstrapTable('refresh',{url :baseUrl+ '/recommends'});
		}
		else{
			$("#_table").bootstrapTable('refresh',{url : baseUrl+'/recommends/selectByFuzeren/' + fuzerenId });
		}
	};
})
var check = function() {

}
var doDelete = function() {
	var rows = $('#_table').bootstrapTable('getSelections');
	if(rows == [] || rows == null || rows.length == 0) {
		parent.layer.alert('请选择要编辑的数据！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 2000
		});
		return;
	}
	if(rows.length != 1) {
		parent.layer.alert('请选择单条数据进行编辑！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 1800
		});
		return;
	}
	$.ajax({
		type: "delete",
		url:baseUrl+ '/recommends/' + rows[0].studentId,
		async: true,
		success: function(data) {
			alert(data.msg);
			$('#_table').bootstrapTable('refresh');
		}
	});
}

var detail = function(uid) {
    parent.layer.open({
        //				title: false,
        maxmin: true,
        type: 2,
        area: modalArea,
        content: './views/form/StudentsDetail.html?studentId='+uid, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        btn: [ '关闭'],
        btn2: function(index, layero) {
        }
    });
}
var insert = function() {
	parent.layer.open({
		//				title: false,
		maxmin: true,
		type: 2,
		area: modalArea,
		content: './views/form/RecommendEdit.html',
		btn: ['保存', '取消'],
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();
			console.log(layero);
			$.ajax({
				url:baseUrl+ '/recommends',
				type: 'POST',
				data: data,
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
			//按钮【按钮二】的回调
			console.log(layero);
			//return false 开启该代码可禁止点击该按钮关闭
		}
	});
}

var edit = function() {
	var rows = $('#_table').bootstrapTable('getSelections');
	if(rows == [] || rows == null || rows.length == 0) {
		parent.layer.alert('请选择要编辑的数据！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 2000
		});
		return;
	}
	if(rows.length != 1) {
		parent.layer.alert('请选择单条数据进行编辑！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 1800
		});
		return;
	}
	parent.layer.open({
		//				title: false,
		maxmin: true,
		type: 2,
		area: modalArea,
		content: './views/form/RecommendEdit.html?studentId='+rows[0].studentId, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		btn: ['保存', '取消'],
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();
			console.log(layero);
			$.ajax({
				url:baseUrl+'/recommends/'+rows[0].studentId,
				type: 'POST',
				data: data,
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
			//按钮【按钮二】的回调
			alert(index);
			console.log(layero);
			//return false 开启该代码可禁止点击该按钮关闭
		}
	});
}

//2018-9-18 chenbin: 新增，绑定负责人下拉框
function bindFuzerenSelect2() {
	var fuzerenData = [];
	$.ajax({
		type : "get",
		url :baseUrl+ '/admins/select2',
		async : false,
		success : function(data) {
			//console.log(data);
			fuzerenData = data;
			fuzerenData.unshift({
				id : 'all',
				text : '请选择'
			});
		}
	});

	$('#_fuzerenSelect').select2({
		data : fuzerenData
	});
}

// 2018-9-26 chenbin: 新增，跳转至“推荐记录管理”页面
function recommendRecord() {
	// 2018-9-26 chenbin: 页面跳转
	window.location.href='RecommendRecord.html'; 
}

//2018-9-26 chenbin: 新增，跳转至“推荐信息”页面
function recommendMsg(studentId) {
	window.location.href='RecommendMsg.html?studentid='+studentId;
}
